﻿using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.SheepFarmes
{
    public class SheepFarmsBackFillHandler : IRequestHandler<SheepFarmsBackFillCommand, Result>
    {
        private readonly IBaseRepository<SheepFarm> _sheepFarmRepository;
        private readonly IMapper _mapper;

        public SheepFarmsBackFillHandler(IBaseRepository<SheepFarm> sheepFarmRepository, IMapper mapper)
        {
            _sheepFarmRepository = sheepFarmRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepFarmsBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var sheepFarms = await (await _sheepFarmRepository.GetAll()).Where(c => c.FarmId == request.FarmId).FirstOrDefaultAsync();
                result.Data = sheepFarms;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
